<template>
    <div class="Breadcrumb">
        <el-breadcrumb separator="/" class="biaoqian">
            <el-breadcrumb-item :to="{ path: '/registrarInterface' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>教材管理</el-breadcrumb-item>
        </el-breadcrumb>
        <el-divider></el-divider>
    </div>
    <div class="showUnTicketInfo">
        <div class="box">
            <el-form :model="queryForm" size="middle" :inline="true" style="padding-left: 20px;  ">
                <el-form-item label="课程名称">
                    <el-input placeholder="请输入课程名称" v-model="queryForm.course" clearable style="width: 240px;" />
                </el-form-item>
                <el-form-item label="标准书号">
                    <el-input placeholder="请输入标准书号" v-model="queryForm.isbn" clearable style="width: 240px" />
                </el-form-item>
                <el-form-item label="教材名称">
                    <el-input placeholder="请输入教材名称" v-model="queryForm.textbook" clearable style="width: 240px" />
                </el-form-item>
                <el-form-item label="出版社">
                    <el-input placeholder="请输入出版社" v-model="queryForm.publisher" clearable style="width: 240px" />
                </el-form-item>
                <el-form-item>
                    <el-row :gutter="10" class="mb8">
                        <el-col :span="1.5">
                            <el-button @click="handleSearch" type="primary" :icon="Search" size="mini">搜索</el-button>
                        </el-col>
                        <el-col :span="1.5">
                            <el-button @click="handleReset" :icon="Refresh" size="mini">重置</el-button>
                        </el-col>
                    </el-row>
                </el-form-item>
                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" :icon="Plus" size="mini" @click="handleAdd">添加教材</el-button>
                    </el-col>
                </el-row>
            </el-form>

        </div>
        <div class="box-content">
            <el-table :data="dataList" height="450" style="width: 100%">
                <el-table-column label="课程名称" prop="courseName" align="center" />
                <el-table-column label="标准书号" prop="isbn" align="center" />
                <el-table-column label="教材名称" prop="name" align="center" />
                <el-table-column label="出版社" prop="publisher" align="center" />
                <el-table-column label="编者" prop="author" align="center" />
                <el-table-column label="估价" prop="price" align="center" />
                <el-table-column label="出版时间" prop="publishDate" align="center" />
                <el-table-column label="班级" prop="className" align="center" />
                <el-table-column label="操作" width="100px" align="center">
                    <template #default="scope">
                        <el-button @click="handleEdit(scope.row)" :icon="Edit" size="mini"
                            style="width: 5px; height: 1px;  "></el-button>
                        <el-popconfirm title="确定删除吗" confirm-button-text="确定" cancel-button-text="取消" icon="InfoFilled"
                            @confirm="handleDelete(scope.row)" hide-after="0">
                            <template #reference>
                                <el-button :icon="Delete" size="mini" style="width: 1px; height: 1px; "></el-button>
                            </template>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination layout="prev, pager, next" :total=pageTotal :page-size="pageSize"
                    v-model:current-page="curPage" />
            </div>
            <TextbookDialog :showDialog="showDialog" @dialogClosed="handleDialogClosed"
                :dialogType="dialogType" :applyId="applyId" :editForm="editForm"></TextbookDialog>
        </div>
    </div>

</template>

<script>
import api from '@/api/api';
import TextbookDialog from '@/components/RegistrarComponents/TextbookDialog.vue';
import { Delete, Edit, Search, Share, Upload, Refresh, Plus } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus';

export default {
    components:{
        TextbookDialog
    },
    mounted() {
        this.getData()
    },
    data() {
        return {
            dataList: [],
            curPage: 1,
            pageSize: 5,
            pageTotal: 0,
            queryForm: {
                course: '',
                isbn: '',
                textbook: '',
                publisher: ''
            },
            showDialog: false,
            dialogType: 'add',
            applyId: '',
            editForm: ''
        }
    },
    methods: {
        getData() {
            api.textbook.page(this.curPage, this.pageSize, this.queryForm.course,
                this.queryForm.isbn, this.queryForm.textbook, this.queryForm.publisher)
                .then((res) => {
                    this.dataList = res.data.data.records
                    this.pageTotal = res.data.data.total
                })
                .catch((err) => {
                    console.log(err)
                })
        },
        handleSearch() {
            this.getData()
        },
        handleReset() {
            this.queryForm.course = ''
            this.queryForm.isbn = ''
            this.queryForm.textbook = ''
            this.queryForm.publisher = ''
            this.getData()
        },
        handleAdd() {
            this.dialogType = 'add'
            this.showDialog = true
        },
        handleDialogClosed() {
            this.showDialog = false
            this.getData()
        },
        handleEdit(row) {
            this.editForm = JSON.parse(JSON.stringify(row))
            this.applyId = row.id
            this.dialogType = 'edit'
            this.showDialog = true
        },
        handleDelete(row) {
            api.textbook.del(row.id).then(() => {
                ElMessage({
                    message: '删除成功',
                    type: 'success'
                })
                this.getData()
            }).catch((err) => {
                console.log(err)
            })
        }
    },
    watch: {
        curPage(newValue, oldValue) {
            this.getData()
        }
    },
    computed: {
        Search() {
            return Search
        },
        Refresh() {
            return Refresh
        },
        Plus() {
            return Plus
        },
        Edit() {
            return Edit
        },
        Delete() {
            return Delete
        }
    }
}
</script>